﻿// <copyright file="PagesDymanicNodeProvider.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Cms.Services.Infrastructure
{
    using MvcSiteMapProvider;
    using System;
    using System.Linq;
    using System.Collections.Generic;

    using CleverCMS.Core.Infrastructure;

    public class PagesDymanicNodeProvider : DynamicNodeProviderBase
    {
        private PageService         m_pagesService;
        private readonly AppCache   m_appCache;

        public PagesDymanicNodeProvider()
        {
            this.m_appCache = new AppCache();
        }

        public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
        {
            if (m_appCache.Exists("Pages"))
               return (List<DynamicNode>)m_appCache.Get("Pages");

            m_pagesService = IoC.Get<PageService>();
            var nodes = new List<DynamicNode>();

            var pages = m_pagesService.LoadAll();
            foreach (var page in pages)
            {
                var pageNode = new DynamicNode { Key = "page_" + Guid.NewGuid(), Title = page.PageTitle };
                pageNode.RouteValues.Add("id", 0);
                pageNode.RouteValues.Add("name", page.Alias);
                pageNode.Controller = "home";
                pageNode.Action = "content";
                nodes.Add(pageNode);
            }

            m_appCache.Add("Pages", nodes);
            return nodes;
        }
    }
}
